<?php
declare (strict_types=1);

namespace app\admin\validate\promotion;

use think\Validate;

/**
 * Class Promotion
 *
 * @package app\admin\validate\market
 */
class Promotion extends Validate
{
    /**
     * 定义验证规则
     * 格式：'字段名'    =>    ['规则1','规则2'...]
     *
     * @var array
     */
    protected $rule = [
        'id'        => 'require',
        'type'      => 'in:1,2,3,4',
        'course' => 'require',
        'time'      => 'require',
        'discount'  => 'requireIf:product_type_id,1',
        'price'     => 'requireCallback:checkPrice',
        'num'       => 'requireCallback:checkNum|number',
    ];

    /**
     * 定义错误信息
     * 格式：'字段名.规则名'    =>    '错误信息'
     *
     * @var array
     */
    protected $message = [
        'type'               => '活动类型不合法',
        'course'          => '请选择课程',
        'time'               => '请选择活动时间',
        'discount.requireIf' => '请填写折扣',
        'price'              => '请填写价格',
        'num'                => '请设置人数'
    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'create' => ['type', 'course', 'time', 'discount', 'price', 'num'],
        'edit'   => ['id', 'type', 'course', 'time', 'discount', 'price', 'num'],
    ];

    /**
     * 验证价格
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/4/15
     *
     * @param $value
     * @param $data
     * @return bool
     */
    public function checkPrice($value, $data)
    {
        if (in_array($data['product_type_id'], [2, 3, 4])) {
            return true;
        }

        return false;
    }

    /**
     * 验证数量
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/4/15
     *
     * @param $value
     * @param $data
     * @return bool
     */
    public function checkNum($value, $data)
    {
        if (in_array($data['product_type_id'], [3, 4])) {
            return true;
        }

        return false;
    }
}